using UnityEngine;

public class Map : MonoBehaviour
{
	public static byte[,] grid;

	public static int height;

	public static int width;

	public static Vector2Int FindNearlestEmptyPoint(Vector3 position, GridType gridType)
	{
		Vector2Int vector2Int = (position / 2f).ToVector2Int();
		byte b = (byte)gridType;
		if (!IsOutOfRange(vector2Int.x, vector2Int.y) && grid[vector2Int.x, vector2Int.y] < b)
		{
			return vector2Int * 2;
		}
		int num = 0;
		ushort num2 = 0;
		while (num++ < 25)
		{
			ushort num3 = 1;
			ushort num4 = 0;
			for (int i = -num2; i <= num2; i++)
			{
				for (int j = -num4; j <= num4; j += num3)
				{
					Vector2Int vector2Int2 = vector2Int;
					vector2Int2.x += j;
					vector2Int2.y += i;
					if (!IsOutOfRange(vector2Int2.x, vector2Int2.y) && grid[vector2Int2.x, vector2Int2.y] < b)
					{
						return vector2Int2 * 2;
					}
				}
				num4 -= (ushort)Mathf.Sign(i);
				num3 = (ushort)(num4 * 2);
				num3 = (ushort)((num3 == 0) ? 1 : num3);
			}
			num2++;
		}
		return vector2Int * 2;
	}

	public static void CreatePlant(Vector2Int point)
	{
		grid[point.x / 2, point.y / 2] = 1;
	}

	public static void DestroyPlant(Vector2Int point)
	{
		grid[point.x / 2, point.y / 2] = 0;
	}

	public static bool IsEmpty(Vector3 position, GridType gridType)
	{
		int num = Mathf.RoundToInt(position.x / 2f);
		int num2 = Mathf.RoundToInt(position.y / 2f);
		return !IsOutOfRange(num, num2) && (int)grid[num, num2] < (int)gridType;
	}

	private static bool IsOutOfRange(Vector2Int point)
	{
		if (point.x < 0 || point.x >= width || point.y < 0 || point.y >= height)
		{
			return true;
		}
		return false;
	}

	private static bool IsOutOfRange(int x, int y)
	{
		if (x < 0 || x >= width || y < 0 || y >= height)
		{
			return true;
		}
		return false;
	}

	public static void SetGrid()
	{
		Transform transform = new GameObject().transform;
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				if ((bool)Physics2D.OverlapPoint(new Vector2(i, j) * 2f, 32785))
				{
					grid[i, j] = 2;
				}
			}
		}
	}
}
